"""
@Project:study_platform
@File:serializer.py
@Author:韩晓雷
@Date:15:23
"""


import re
from rest_framework import serializers
from .models import *
from rest_framework.validators import ValidationError


# 标签序列化器
class TagSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = TagModel
        fields = "__all__"

    # 重写校验方法
    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"[A-Za-z]{2,9}\d{2,9}", name):
            raise ValidationError(f'{name}标签由2到9为字母组成')

        return attrs


# 分类序列化器
class ClassifySer(serializers.ModelSerializer):

    class Meta:
        model = ClassIfyModel
        fields = "__all__"

    # 重写校验方法
    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"[A-Za-z]{2,9}\d{1,9}", name):
            raise ValidationError(f'{name}分类由2到9为字母组成')

        return attrs


# 学习内容序列化器
class ContentsSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    classify = serializers.PrimaryKeyRelatedField(queryset=ClassIfyModel.objects.all())
    classify_name = serializers.SerializerMethodField()

    def get_classify_name(self, obj):
        return obj.classify.name

    class Meta:
        model = ContentsModel
        fields = "__all__"


# 学习资源
class WealthSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = WealthModel
        fields = "__all__"


# 学习任务
class TaskSer(serializers.ModelSerializer):
    content = serializers.PrimaryKeyRelatedField(queryset=ContentsModel.objects.all())
    content_title = serializers.SerializerMethodField()

    def get_content_title(self, obj):
        return obj.content.title

    classs = serializers.PrimaryKeyRelatedField(queryset=ClasssModel.objects.all())
    classs_name = serializers.SerializerMethodField()

    def get_classs_name(self, obj):
        return obj.classs.name

    class Meta:
        model = TaskModel
        fields = "__all__"


# 学习记录
class RecordSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = RecordModel
        fields = "__all__"


# 评论系列化器
class CommentSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    contents = serializers.PrimaryKeyRelatedField(queryset=ContentsModel.objects.all())
    contents_title = serializers.SerializerMethodField()

    def get_contents_title(self, obj):
        return obj.contents.title

    class Meta:
        model = CommentModel
        fields = "__all__"


# 弹幕系列化器
class BarrageSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    contents = serializers.PrimaryKeyRelatedField(queryset=ContentsModel.objects.all())
    contents_title = serializers.SerializerMethodField()

    def get_contents_title(self, obj):
        return obj.contents.title

    class Meta:
        model = BarrageModel
        fields = "__all__"


# 订单系列化器
class OrderSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = OrderModel
        fields = "__all__"


# 收藏系列化器
class CollectSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    user_account = serializers.SerializerMethodField()

    def get_user_account(self, obj):
        return obj.user.account

    wealth = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    wealth_name = serializers.SerializerMethodField()

    def get_wealth_name(self, obj):
        return obj.wealth.name

    class Meta:
        model = CollectModel
        fields = "__all__"


